package org.jackysoft.service.impl;

import java.io.File;
import java.io.StringReader;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.hibernate.Query;
import org.hibernate.Session;
import org.jackysoft.data.SortInfo;
import org.jackysoft.opensymphony.workflow.entity.WorkflowDescriptor;
import org.jackysoft.opensymphony.workflow.entity.WorkflowName;
import org.jackysoft.service.AbstractIdentifiedService;
import org.jackysoft.service.VariableSettingService;
import org.jackysoft.service.WorkflowNameService;
import org.jackysoft.service.WorkflowService;
import org.jackysoft.util.IgnoreDtdValidateResolver;
import org.jackysoft.util.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class WorkflowNameServiceImpl 
extends AbstractIdentifiedService<WorkflowName>
		implements WorkflowNameService {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8259420893370149525L;
	private WorkflowService workflowService;
	private VariableSettingService  variableSettingService;
	
	public WorkflowNameServiceImpl() {
		super(WorkflowName.class);
	}

	@Override
	public Iterable<WorkflowName> getData(int start, int size,
			boolean listAll, SortInfo sortInfo, Map<String, Object> smap) {
		Session sess = getSession();
		String hql = "from WorkflowName";
		// hql = "select new WorkflowName(w.workflowName) from WorkflowName";
		Query crt = sess.createQuery(hql);
		if (!listAll) {
			crt.setFirstResult(start);
			crt.setMaxResults(size);
		}
		return crt.list();
	}

	@Override
	public int getSize(SortInfo sortInfo, Map<String, Object> smap) {
		Session sess = getSession();
		Query crt = sess.createQuery("select count(*) from WorkflowName");
		Object obt = crt.uniqueResult();
		return Integer.parseInt(obt.toString());
	}

	@Override
	public WorkflowName getByWorkflowName(WorkflowName name) {
		WorkflowName wfn = null;
		Session session = getSession();
		wfn = (WorkflowName) session.get(WorkflowName.class,
				name.getWorkflowName());
		return wfn;
	}

	@Override
	public void remove(WorkflowName name) {
		
		   WorkflowName wn = get(name.getWorkflowName());
		   variableSettingService.removeByWorkflow(name.getWorkflowName());
	       super.remove(wn);
	       workflowService.getWorkflowCaches().remove(name.getWorkflowName());
	}

	@Override
	public void update(WorkflowName name, File temp, String fileName) throws DocumentException {
		SAXReader sreader = new SAXReader();
		sreader.setEntityResolver(new IgnoreDtdValidateResolver());
		Document doc = sreader.read(temp);
		name = get(name.getWorkflowName());
		WorkflowDescriptor wdesc = new WorkflowDescriptor(doc.getRootElement());
		name.setWorkflowName(StringUtils.splitPreFileName(fileName));
		name.setWorkflowDescriptor(wdesc);
		super.update(name);

	}

	@Override
	public void create(File temp, String fileName) throws Exception {
		SAXReader sreader = new SAXReader();
		sreader.setEntityResolver(new IgnoreDtdValidateResolver());
		Document doc = sreader.read(temp);
		WorkflowDescriptor wdesc = new WorkflowDescriptor(doc.getRootElement());
		WorkflowName wfn = new WorkflowName();
		wfn.setWorkflowName(StringUtils.splitPreFileName(fileName));
		wfn.setWorkflowDescriptor(wdesc);
		log.info(wfn.getWorkflowName()+wdesc.asXML());
		create(wfn);
		Map<String, WorkflowDescriptor> workflows = workflowService
				.getWorkflowCaches();
		workflows.put(wfn.getWorkflowName(), wdesc);
	}

	

	@Resource
	public void setWorkflowService(WorkflowService workflowService) {
		this.workflowService = workflowService;
	}

	@Override
	public String getDefineXml(String name) throws DataAccessException {
		
		WorkflowDescriptor desc = this.workflowService.getWorkflowDescriptor(name);
		String xml = desc.asXML();
		return xml;
	}

	@Override
	public void updateOfContent(String workflowName, String content)
			throws DataAccessException, DocumentException {
		 WorkflowName wname = get(workflowName);
		 SAXReader sreader = new SAXReader();	
		 Document doc = sreader.read(new StringReader(content));
		 WorkflowDescriptor desc = new  WorkflowDescriptor(doc.getRootElement());
		 wname.setWorkflowDescriptor(desc);
		 update(wname);		
	}

	@Resource
	public void setVariableSettingService(VariableSettingService psettingService) {
		this.variableSettingService = psettingService;
	}

	@Override
	public Collection<WorkflowName> getSimpleifyWorkflows() {
		String hql = String.format("select new WorkflowName(workflowName,imagePath,remark,enabled) from %s", entitytable());
		List list = query(hql).list();
		return list;
	}

}
